import argparse
import asyncio
import logging
import os
import sys

import version_util
from logger import log
from settings import settings

if __name__ == '__main__':
    try:
        pid = os.getpid()
        logging.info(f"程序开始运行 {pid}")
        parser = argparse.ArgumentParser(description='manual to this script')
        parser.add_argument("--code", type=str, default="lipeng_test")
        parser.add_argument("--host", type=str, default="platform.wechain360.com")
        parser.add_argument("--vs_host", type=str, default="mq.wechain360.com")
        parser.add_argument("--image", type=str, default="False")
        parser.add_argument("--force_codec", type=str, default="")
        parser.add_argument("--ob", type=str, default="False")
        parser.add_argument("--video_client_server", type=str, default="False")
        args = parser.parse_args()
        log.info(f"code:{args}")
        # 初始化配置
        settings.init(args.code, args.host, args.force_codec, args.vs_host)
        if args.video_client_server == "True":
            import video_client_server
            video_client_server.start()
            sys.exit(0)

        import metric
        import mqtt
        from objectdetection import objectdetection
        from snapshot import snapshot
        from camera_check import camera_check

        # 开启离线检测
        metric.init()
        # 开启mqtt服务
        mqtt.init()
        # 开启抓拍服务
        snapshot.init()
        # 开启摄像头检测服务
        camera_check.init()
        # 开启定时更新检查
        version_util.version_check(pid, settings.host, settings.id)
        # 开启检物体检测服务
        if args.ob == "True":
            logging.info("初始化 yolov5")
            objectdetection.start()
        logging.info("程序成功运行结束")
        if args.image == "True":
            # 开启视频转发服务
            import video_client

            video_client.start()
        else:
            # 开启视频预览服务
            from webcam import socket_logic

            asyncio.run(socket_logic())

    except Exception as e:
        logging.exception("程序遇到错误")
